package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.RefundPageQueryDTO;
import com.sky.entity.Payment;
import com.sky.entity.Refund;
import com.sky.mapper.RefundMapper;
import com.sky.result.PageResult;
import com.sky.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RefundServiceImpl implements RefundService {

    @Autowired
    private RefundMapper refundMapper;

    @Override
    // 查询所有需要退款的订单
    public PageResult pageQuery(RefundPageQueryDTO refundPageQueryDTO) {
        // 设置分页参数
        PageHelper.startPage(refundPageQueryDTO.getPage(),refundPageQueryDTO.getPageSize());   //设置分页查询参数

        // 调用mapper查询数据
        Page<Refund> page = refundMapper.pageQuery(refundPageQueryDTO);

        Long total=page.getTotal(); // 满足条件的总记录数
        List<Refund> paymentList=page.getResult(); //满足条件的所有记录

        return new PageResult(total,paymentList);
    }


    // 申请退款订单
    @Override
    public boolean accept(Long orderId) {
        // 修改订单状态
        refundMapper.updatePaymentStatus(orderId);
        refundMapper.updateOrderStatus(orderId);
        return true;
    }

    // 拒绝退款订单
    @Override
    public void refuse(Long orderId) {
        // 退款失败，修改订单状态
        refundMapper.updateOrderStatusRefuse(orderId);
        refundMapper.updatePaymentStatusRefuse(orderId);
    }
}
